<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace AlibabaCloud\Client\Request;

abstract class Request implements \ArrayAccess
{
    use Traits\DeprecatedTrait;
    use \AlibabaCloud\Client\Traits\HttpTrait;
    use \AlibabaCloud\Client\Traits\RegionTrait;
    use Traits\ClientTrait;
    use Traits\AcsTrait;
    use \AlibabaCloud\Client\Traits\ArrayAccessTrait;
    use \AlibabaCloud\Client\Traits\ObjectAccessTrait;
    use Traits\RetryTrait;
    public $method = "GET";
    public $format = "JSON";
    protected $scheme = "http";
    public $client;
    public $uri;
    public $data = [];
    private $userAgent = [];
    const CONNECT_TIMEOUT = 5;
    const TIMEOUT = 10;
    public function __construct(array $options = [])
    {
        $this->client = \AlibabaCloud\Client\Credentials\Providers\CredentialsProvider::getDefaultName();
        $this->uri = new \GuzzleHttp\Psr7\Uri();
        $this->uri = $this->uri->withScheme($this->scheme);
        $this->options["http_errors"] = false;
        $this->options["connect_timeout"] = self::CONNECT_TIMEOUT;
        $this->options["timeout"] = self::TIMEOUT;
        if(strtolower(AlibabaCloud\Client\env("DEBUG")) === "sdk") {
            $this->options["debug"] = true;
        }
        if($options !== []) {
            $this->options($options);
        }
    }
    public function appendUserAgent($name, $value)
    {
        $filter_name = \AlibabaCloud\Client\Filter\Filter::name($name);
        if(!UserAgent::isGuarded($filter_name)) {
            $this->userAgent[$filter_name] = \AlibabaCloud\Client\Filter\Filter::value($value);
        }
        return $this;
    }
    public function withUserAgent(array $userAgent)
    {
        $this->userAgent = UserAgent::clean($userAgent);
        return $this;
    }
    public function format($format)
    {
        $this->format = \AlibabaCloud\Client\Filter\ApiFilter::format($format);
        return $this;
    }
    public function contentType($contentType)
    {
        $this->options["headers"]["Content-Type"] = \AlibabaCloud\Client\Filter\HttpFilter::contentType($contentType);
        return $this;
    }
    public function accept($accept)
    {
        $this->options["headers"]["Accept"] = \AlibabaCloud\Client\Filter\HttpFilter::accept($accept);
        return $this;
    }
    public function body($body)
    {
        $this->options["body"] = \AlibabaCloud\Client\Filter\HttpFilter::body($body);
        return $this;
    }
    public function jsonBody($content)
    {
        if(!is_array($content) && !is_object($content)) {
            throw new \AlibabaCloud\Client\Exception\ClientException("jsonBody only accepts an array or object", \AlibabaCloud\Client\SDK::INVALID_ARGUMENT);
        }
        return $this->body(json_encode($content));
    }
    public function scheme($scheme)
    {
        $this->scheme = \AlibabaCloud\Client\Filter\HttpFilter::scheme($scheme);
        $this->uri = $this->uri->withScheme($scheme);
        return $this;
    }
    public function host($host)
    {
        $this->uri = $this->uri->withHost(\AlibabaCloud\Client\Filter\HttpFilter::host($host));
        return $this;
    }
    public function method($method)
    {
        $this->method = \AlibabaCloud\Client\Filter\HttpFilter::method($method);
        return $this;
    }
    public function client($clientName)
    {
        $this->client = \AlibabaCloud\Client\Filter\ClientFilter::clientName($clientName);
        return $this;
    }
    public function isDebug()
    {
        if(isset($this->options["debug"])) {
            return $this->options["debug"] === true;
        }
        if(isset($this->httpClient()->options["debug"])) {
            return $this->httpClient()->options["debug"] === true;
        }
        return false;
    }
    public function resolveOption()
    {
        $this->options["headers"]["User-Agent"] = UserAgent::toString($this->userAgent);
        $this->cleanQuery();
        $this->cleanFormParams();
        $this->resolveHost();
        $this->resolveParameter();
        if(isset($this->options["form_params"])) {
            $this->options["form_params"] = GuzzleHttp\Psr7\parse_query(\AlibabaCloud\Client\Encode::create($this->options["form_params"])->toString());
        }
        $this->mergeOptionsIntoClient();
    }
    public function request()
    {
        $this->resolveOption();
        $result = $this->response();
        if($this->shouldServerRetry($result)) {
            return $this->request();
        }
        if(!$result->isSuccess()) {
            throw new \AlibabaCloud\Client\Exception\ServerException($result);
        }
        return $result;
    }
    public function requestAsync()
    {
        $this->resolveOption();
        return self::createClient($this)->requestAsync($this->method, (string) $this->uri, $this->options);
    }
    public static function createClient(Request $request = NULL)
    {
        if(\AlibabaCloud\Client\AlibabaCloud::hasMock()) {
            $stack = \GuzzleHttp\HandlerStack::create(\AlibabaCloud\Client\AlibabaCloud::getMock());
        } else {
            $stack = \GuzzleHttp\HandlerStack::create();
        }
        if(\AlibabaCloud\Client\AlibabaCloud::isRememberHistory()) {
            $stack->push(\GuzzleHttp\Middleware::history(\AlibabaCloud\Client\AlibabaCloud::referenceHistory()));
        }
        if(\AlibabaCloud\Client\AlibabaCloud::getLogger()) {
            $stack->push(\GuzzleHttp\Middleware::log(\AlibabaCloud\Client\AlibabaCloud::getLogger(), new \AlibabaCloud\Client\Log\LogFormatter(\AlibabaCloud\Client\AlibabaCloud::getLogFormat())));
        }
        $stack->push(\GuzzleHttp\Middleware::mapResponse(function (\Psr\Http\Message\ResponseInterface $response) use($request) {
            return new \AlibabaCloud\Client\Result\Result($response, $request);
        }));
        self::$config["handler"] = $stack;
        return new \GuzzleHttp\Client(self::$config);
    }
    private function response()
    {
        try {
            return self::createClient($this)->request($this->method, (string) $this->uri, $this->options);
        } catch (\GuzzleHttp\Exception\GuzzleException $exception) {
            if($this->shouldClientRetry($exception)) {
                return $this->response();
            }
            throw new \AlibabaCloud\Client\Exception\ClientException($exception->getMessage(), \AlibabaCloud\Client\SDK::SERVER_UNREACHABLE, $exception);
        }
    }
    private function cleanQuery()
    {
        if(isset($this->options["query"]) && $this->options["query"] === []) {
            unset($this->options["query"]);
        }
    }
    private function cleanFormParams()
    {
        if(isset($this->options["form_params"]) && $this->options["form_params"] === []) {
            unset($this->options["form_params"]);
        }
    }
}

?>